﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class StockReport : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        public string ConvestToDateN(string s)
        {
            string day = "";
            string mount = "";
            string years = "";

            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) < 2557)
                {
                    years = (Convert.ToInt32(years) + 543).ToString();
                }

                return (day + "/" + mount + "/" + years);
            }
            catch (Exception)
            {

            }
            return (day + "/" + mount + "/" + years);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string Stock_ID = Session["Stock_ID"].ToString();
           
            string a = Session["DocDate"].ToString();
            DateTime docDate = Convert.ToDateTime(a);
            string DocDate = ConvestToDateN((docDate.Date).ToShortDateString());

            //string b = Session["DeeadLine"].ToString();
            //DateTime DeadLine = Convert.ToDateTime(a);
            //string DeeadLine = ConvestToDateN((DeadLine.Date).ToShortDateString());
            string PrintDate = ConvestToDateN((DateTime.Now).ToShortDateString());


            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();


            strSQL.Append(" SELECT TB_Stock.Stock_ID, TB_Stock.Stock_doc_NO, TB_Stock.Stock_Date,TB_Stock_List.Product_ID , TB_Stock.Act_Employee_ID, TB_Product.Product_Name, TB_Product_Category.Product_Category_Name, ");
            strSQL.Append(" TB_Tranfer_Category.Transfer_Category_Name, TB_Employee.FirstName, TB_Employee.LastName, TB_Department.Department_Name, TB_Employee.Prefix, TB_Warehouse.Warehouse_name, ");
            strSQL.Append(" TB_Stock_List.Amount");
            strSQL.Append(" FROM TB_Stock LEFT JOIN");
            strSQL.Append(" TB_Stock_List ON TB_Stock.Stock_ID = TB_Stock_List.Stock_ID LEFT JOIN");
            strSQL.Append(" TB_Warehouse ON TB_Stock_List.Warehouse_ID = TB_Warehouse.Warehouse_ID LEFT JOIN");
            strSQL.Append(" TB_Employee ON TB_Stock.Act_Employee_ID = TB_Employee.Employee_ID LEFT JOIN");
            strSQL.Append(" TB_Product ON TB_Stock_List.Product_ID = TB_Product.Product_ID LEFT JOIN");
            strSQL.Append(" TB_Product_Category ON TB_Product.Product_Category_ID = TB_Product_Category.Product_Category_ID LEFT JOIN");
            strSQL.Append(" TB_Tranfer_Category ON TB_Stock_List.Transfer_Category_ID = TB_Tranfer_Category.Transfer_Category_ID LEFT JOIN");
            strSQL.Append(" TB_Department ON TB_Employee.Departmernt_ID = TB_Department.Department_ID");
            strSQL.Append(" WHERE TB_Stock.Stock_ID = " + Stock_ID + " ");



            objConn.ConnectionString = Connection;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;

            dtAdapter.Fill(ds, "CrystalReport11");
            dt = ds.Tables[0];

            dtAdapter = null;
            objConn.Close();
            objConn = null;

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Empoyee_Management/CrystalReport11.rpt"));

            rpt.SetDataSource(dt);
           
            rpt.SetParameterValue("DocDate", DocDate);
            rpt.SetParameterValue("PrintDate", PrintDate);
           
            this.CrystalReportViewer4.ReportSource = rpt;
        }
    }
}